package com.huizhonglm.diap.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.wallet.WxOrderSuccessParams;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.storage.pref.Preferences;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

	private IWXAPI api;

	private TextView tipTxt;
	private ImageView tipImg;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxpay_entry);
		tipTxt = (TextView) findViewById(R.id.wxpay_tip_txt);
		tipImg = (ImageView) findViewById(R.id.wxpay_tip_img);
		View backBtn = findViewById(R.id.title_back_ibtn);
		backBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		api = WXAPIFactory.createWXAPI(this, Constants.WECHAT_APPID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		LogUtil.print(TAG, "onPayFinish, errCode = " + resp.errCode);
		Preferences.getInstance().edit().putInt("wxpayEC", resp.errCode).apply();
		if (tipTxt != null) {
			switch (resp.errCode) {
				case 0:
					tipImg.setImageResource(R.drawable.ic_pay_success);
					tipTxt.setTextColor(0xff333333);
					tipTxt.setText("恭喜您，支付成功！");
					WXOrderSuccess(Constants.WECHAT_ORDER_ID,"1");
					break;
				case -1:
					tipImg.setImageResource(R.drawable.ic_pay_failure);
					tipTxt.setTextColor(0xffcccccc);
					tipTxt.setText("对不起，支付失败！");
					WXOrderSuccess(Constants.WECHAT_ORDER_ID,"0");
					break;
				case -2:
					tipImg.setImageResource(R.drawable.ic_pay_failure);
					tipTxt.setTextColor(0xffcccccc);
					tipTxt.setText("您取消了支付！");
					WXOrderSuccess(Constants.WECHAT_ORDER_ID,"0");
					break;
			}
		}
	}
	/**
	 * 微信支付成功'回调服务器接口告诉服务器
	 */
	private void WXOrderSuccess(String orderId,String status){
		WxOrderSuccessParams params = new WxOrderSuccessParams();
		params.orderId = orderId;
		params.status=status;
		API.http().post("WXOrderSuccess", params, new HttpCallback() {
			@Override
			public void onSuccess(String result) {
				LogUtil.print("WXOrderSuccess -- resp=" + result);
				finish();
			}

			@Override
			public void onFailure(Throwable ex) {
				LogUtil.print("WXOrderSuccess -- resp=" + ex.getMessage());
				finish();
			}
		});
	}
}